package com.younion.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseDAO;
import com.younion.common.base.BaseVO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.AfficheVO;

public class AfficheDAO extends BaseDAO {
	
	protected static final Log logger = LogFactory.getLog(AfficheDAO.class);
	
	public AfficheVO selectAfficheById(Map paramMap) throws BaseException {
		try {
			return (AfficheVO) this.getSqlMapClientTemplate().queryForObject("t_affiche.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public boolean updateAfficheByMap(Map paramMap) throws BaseException {
        try{
        	this.update("t_affiche.updateByMap", paramMap);
        	return true;
        } catch(Exception e) {
        	logger.error(e.getMessage(), e);
        	throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }
	
	public boolean deleteAfficheById(Map paramMap) throws BaseException {
        try{
        	this.getSqlMapClientTemplate().delete("t_affiche.deleteById", paramMap);
        	return true;
        } catch(Exception e) {
        	logger.error(e.getMessage(), e);
        	throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }
	
	public boolean deleteAfficheByIds(List list) throws BaseException {
        try{
        	this.getSqlMapClientTemplate().delete("t_affiche.deleteByIds", list);
        	return true;
        } catch(Exception e) {
        	logger.error(e.getMessage(), e);
        	throw new BaseException(this.getClass().getSimpleName() +" method error");
        }    
    }    
	
	/**
	 * 查询记录数
	 * @param paramMap
	 * @return
	 * @throws BaseException
	 */
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_affiche.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	/**
	 * 实现翻页
	 * 
	 * @param condition
	 *            condition{Vo:条件VO对象，curPage:当前页面数，pageSize:每页最大行数}
	 * @return type : Page 返回一个分页的List，操作失败返回null
	 */
	public List selectForPage(Map condition, Paginator paginator) throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_affiche.selectAll";
			} else {
				sqlStr = "t_affiche.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	/**
	 * 向数据库中插入一个VO对象
	 * 
	 * @param afficheVO
	 *            对象对应到数据库中的一条记录
	 * @return type : AfficheVO 返回操作是否成功
	 */
	public AfficheVO insertByVo(AfficheVO afficheVO) throws BaseException {
		if (afficheVO == null)
			return null;
		try {
			return (AfficheVO) this.insert("t_affiche.insertByVo", (BaseVO) afficheVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public List<AfficheVO> selectWebAffiche(Map condition, Paginator paginator) throws BaseException {
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		try {
			Map vo = (Map) condition.get("paramMap");
			String sqlStr = "t_affiche.selectWebAffiche";
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public List<AfficheVO> selectApiAfficheByMap(Map paramMap) throws BaseException {
		try {
			return (List<AfficheVO>) this.getSqlMapClientTemplate().queryForList("t_affiche.selectApiAfficheByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

}
